package com.thingcom.delinrobot.main.Launch.Login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiSDKListener;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.thingcom.delinrobot.Common.Constant;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.main.adddevice.DeviceListActivity;
import com.thingcom.delinrobot.utils.MyLog;
import com.thingcom.delinrobot.utils.PreferencesUtil;
import com.thingcom.delinrobot.utils.ToastUtil;
import com.thingcom.delinrobot.view.SimpleToolbar;

import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import static com.thingcom.delinrobot.Common.Constant.MAIL_S;
import static com.thingcom.delinrobot.Common.Constant.TOKEN;
import static com.thingcom.delinrobot.Common.Constant.UID;
import static com.thingcom.delinrobot.utils.AppUtils.hideSoftKeyboard;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/12/4
 */
public class LoginPasswordFragment extends BaseBackFragment {
    private static final String TAG = "LoginPasswordFragment";
    private String loginMail;

    @BindView(R.id.et_login_login_password)
    TextInputEditText etLoginLoginPassword;
    @BindView(R.id.til_login_login_password)
    TextInputLayout tilLoginLoginPassword;
    @BindView(R.id.toolbar_login_password)
    SimpleToolbar toolbarLoginPassword;
    @BindView(R.id.tv_mail_address)
    TextView tvMailAddress;
    @BindView(R.id.tv_login_forget_password)
    TextView tvLoginForgetPassword;
    @BindView(R.id.btn_login_password_continue)
    Button btnLoginPasswordContinue;
    @BindView(R.id.cl_login_password)
    ConstraintLayout clLoginPassword;


    public static LoginPasswordFragment newInstance() {
        Bundle args = new Bundle();
        LoginPasswordFragment fragment = new LoginPasswordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_login_password, container, false);
        unbinder = ButterKnife.bind(this, view);
        loginMail = PreferencesUtil.getInstance(getContext()).getString(Constant.MAIL_NUMBER,null);
        initToolbar(toolbarLoginPassword);
        ColorStateList colorStateList = getResources().getColorStateList(R.color.text_input_edit_selectoe);
        tilLoginLoginPassword.setBoxStrokeColorStateList(colorStateList);
        clLoginPassword.setOnTouchListener((v, event) -> {
            etLoginLoginPassword.clearFocus();
            hideSoftKeyboard(getContext(), clLoginPassword);
            return false;
        });
        tvMailAddress.setText(loginMail);
        GizWifiSDK.sharedInstance().setListener(mListener);
        return view;
    }




    @OnClick({R.id.tv_login_forget_password, R.id.btn_login_password_continue})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login_forget_password:

                start(LoginFindPasswordFragment.newInstance());
                break;
            case R.id.btn_login_password_continue:
                String inputPassword = Objects.requireNonNull(etLoginLoginPassword.getText()).toString();
                GizWifiSDK.sharedInstance().userLogin(loginMail,inputPassword);

                hideSoftKeyboard(_mActivity);
//                GizWifiSDK.sharedInstance().userLogin(inputName, inputPassword);
                break;
        }
    }

    // 实现回调
    private GizWifiSDKListener mListener = new GizWifiSDKListener() {
        @Override
        public void didUserLogin(GizWifiErrorCode result, String uid, String token) {
            MyLog.i(TAG, "result: " + result);
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                String inputPassword = Objects.requireNonNull(etLoginLoginPassword.getText()).toString();
                PreferencesUtil.getInstance(getContext()).putString(MAIL_S, inputPassword);
                MyLog.i(TAG, "didUserLogin: " + inputPassword);
                // isLoading=true;
                Intent intent = new Intent(getContext(), DeviceListActivity.class);
                startActivity(intent);
                // 登录成功
            } else if (result == GizWifiErrorCode.GIZ_OPENAPI_USERNAME_PASSWORD_ERROR) {
                ToastUtil.showToast(getContext(), getString(R.string.giz_username_or_password_error));
            }else {
                ToastUtil.showToast(getContext(), getString(R.string.fail_toast));

                // isLoading=false;
                // 登录失败
                PreferencesUtil.getInstance(getContext()).putString(MAIL_S, null);
                MyLog.i("登陆失败", "didUserLogin: " + result);
            }
            PreferencesUtil.getInstance(getContext()).putString(UID, uid);
            PreferencesUtil.getInstance(getContext()).putString(TOKEN, token);
            MyLog.i("登陆", "didUserLogin: " + "UID:" + uid + "TOKEN:"+token);
        }


    };
}
